package 树.easy;

/**
 * @author Lw中
 * @date 2021/7/25 22:24
 */
public class ErSanWu {

    // 235.二叉搜索树的最近公共祖先
    public class TreeNode {
       int val;
       TreeNode left;
       TreeNode right;
       TreeNode(int x) { val = x; }
    }

    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        TreeNode res = root;
        while (true) {
            if (p.val < res.val && q.val < res.val) {
                res = res.left;
            }
            else if(p.val > res.val && q.val > res.val) {
                res = res.right;
            }
            else break;
        }
        return res;
    }

}
